﻿using System;
using System.Windows.Forms;
using System.IO;

namespace EnDecrypter
{
    public partial class MainForm : Form
    {
        ConfigData configData = new ConfigData();
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(openFileDlg.ShowDialog() == DialogResult.OK)
            {
                foreach(string s in openFileDlg.FileNames)
                {
                    listBox1.Items.Add(s);
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            while(listBox1.SelectedIndices.Count > 0)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndices[0]);
            }
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            string outputPath = textBox_DeFolder.Text;
            if (String.IsNullOrWhiteSpace(outputPath))
            {
                if (folderBrowserDlg.ShowDialog() == DialogResult.OK)
                {
                    textBox_DeFolder.Text = folderBrowserDlg.SelectedPath;
                    outputPath = textBox_DeFolder.Text;
                }
                else
                    return;
            }
            if (!outputPath.EndsWith("\\"))
                outputPath += "\\";
            for (int i = 0;i< listBox1.Items.Count;i++)
            {
                string path = listBox1.Items[i].ToString();
                if (configData.ModeType == 0)
                {
                    EncryptHelper.DecryptAES(path, outputPath + Path.GetFileName(path), HexStringToByte(configData.AesKey), HexStringToByte(configData.AesIV));
                }
                else if (configData.ModeType == 1)
                {
                    EncryptHelper.DecryptDES(path, outputPath + Path.GetFileName(path), HexStringToByte(configData.DesKey), HexStringToByte(configData.DesIV));
                }
            }

            MessageBox.Show("所有的文件已经解密完成！", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private byte[] HexStringToByte(string hex)
        {
            string[] key = hex.Split(',');
            byte[] keys = new byte[key.Length];
            for (int j = 0; j < key.Length; j++)
            {
                string s = key[j].Replace("0x", "").Replace(" ", "");
                keys[j] = Convert.ToByte(s,16);
            }
            return keys;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void btnEnFolder_Click(object sender, EventArgs e)
        {
            if(folderBrowserDlg.ShowDialog()==DialogResult.OK)
            {
                textBox_EnFolder.Text = folderBrowserDlg.SelectedPath;
            }
        }

        private void btnDeFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDlg.ShowDialog() == DialogResult.OK)
            {
                textBox_DeFolder.Text = folderBrowserDlg.SelectedPath;
            }
        }

        private void btnOpenEnFolder_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(textBox_EnFolder.Text))
                System.Diagnostics.Process.Start("Explorer.exe"," "+textBox_EnFolder.Text);
        }

        private void btnOpenDeFolder_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(textBox_DeFolder.Text))
                System.Diagnostics.Process.Start("Explorer.exe", " " + textBox_DeFolder.Text);
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            string outputPath = textBox_EnFolder.Text;
            if (String.IsNullOrWhiteSpace(outputPath))
            {
                if (folderBrowserDlg.ShowDialog() == DialogResult.OK)
                {
                    textBox_EnFolder.Text = folderBrowserDlg.SelectedPath;
                    outputPath = textBox_EnFolder.Text;
                }
                else
                    return;
            }
            if (!outputPath.EndsWith("\\"))
                outputPath += "\\";
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                string path = listBox1.Items[i].ToString();
                if (configData.ModeType == 0)
                {
                    EncryptHelper.EncryptAES(path, outputPath + Path.GetFileName(path), HexStringToByte(configData.AesKey), HexStringToByte(configData.AesIV));
                }
                else if (configData.ModeType == 1)
                {
                    EncryptHelper.EncryptDES(path, outputPath + Path.GetFileName(path), HexStringToByte(configData.DesKey), HexStringToByte(configData.DesIV));
                }
            }

            MessageBox.Show("所有的文件已经加密完成！", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btn_Setting_Click(object sender, EventArgs e)
        {
            SettingForm stForm = new SettingForm();
            stForm.config = configData;
            if (stForm.ShowDialog() == DialogResult.OK)
                configData = stForm.config;
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            if(!File.Exists(Config.configPath))
            {
                SettingForm stForm = new SettingForm();
                if (stForm.ShowDialog() == DialogResult.OK)
                    configData = stForm.config;
            }
            else
            {
                configData = Config.XMLDeserialize(Config.configPath);
            }
        }
    }
}
